package distributedRE.model;

import informationExtractionSystem.InformationExtractor;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import configuration.parameters.DoubleValuedParameter;
import configuration.parameters.Parameter;

public class DistributedREModel {

	private static long NEXT_ID = 0;
	private ArrayList<Session> allsessions;
	private ArrayList<Session> sessions;
	private Session lastSession;
	private ArrayList<DataBase> databases;
	private ArrayList<Query> queries;
	private long id;
	private Hashtable<DataBase, ArrayList<Query>> dbqueries;
	private InformationExtractor iE;
	private Relation relation;
	
	private DistributedREModel(ArrayList<Session> ss){
		allsessions = ss;
		
		sessions = new ArrayList<Session>();
		
		for (int i = 0; i < allsessions.size()-1; i++) {
			sessions.add(allsessions.get(i));
			
		}
		
		lastSession = allsessions.get(allsessions.size()-1);
		id = NEXT_ID++;
		dbqueries = new Hashtable<DataBase, ArrayList<Query>>();
	}
	
	public DistributedREModel(){
		sessions = new ArrayList<Session>();
		allsessions = new ArrayList<Session>();
		lastSession = null;
		id = NEXT_ID++;
		dbqueries = new Hashtable<DataBase, ArrayList<Query>>();
	}
	
	public ArrayList<DataBase> getDatabases() {
		return databases;
	}

	public ArrayList<Query> getQueries(DataBase dataBase) {
		
		return dbqueries.get(dataBase);
	
	}

	public void append(Session s) {
		
		allsessions.add(s);
		
		if (lastSession!=null)
			sessions.add(lastSession);
		
		lastSession = s;
		
	}
	
	public Session getLastSession(){
		return lastSession;
	}

	public ArrayList<Session> getAllSessions() {
		
		return allsessions;
	}

	public DistributedREModel getModelatSession(int i){
		
		List<Session> l = allsessions.subList(0, i);
		
		ArrayList<Session> ret = new ArrayList<Session>();
		
		for (Session session : l) {
			ret.add(session);
		}
		
		DistributedREModel retu = new DistributedREModel(ret);
		
		retu.setRelation(getRelation());
		
		retu.setInformationExtractor(getInformationExtractor());
		
		return retu;
	}
	
	public int getNumberOfSessions(){
		return allsessions.size();
	}

	public long getId() {
		return id;
	}

	public void setDatabases(ArrayList<DataBase> databases) {
		
		this.databases = databases;
		
	}

	public InformationExtractor getInformationExtractor() {
		return iE;
	}

	public void setInformationExtractor(InformationExtractor iE) {
		
		this.iE = iE;
		
	}
	
	public boolean equals(Object o){
		return (id == ((DistributedREModel)o).id);
	}

	public void setQueryForDataBase(Query query, DataBase dataBase, double ip) {
		
		ArrayList<Query> queries = dbqueries.get(dataBase);
		
		if (queries == null){
			queries = new ArrayList<Query>();
		}
		
		queries.add(query);
		
		dbqueries.put(dataBase, queries);
		
		QueryDatabase qdb = QueryDatabase.getInstance(query, dataBase, this);
		
		qdb.addParameter(Parameter.PRECISION, new DoubleValuedParameter(ip));
		
	}

	public void setRelation(Relation relation) {
		
		this.relation = relation;
		
	}
	
	public Relation getRelation(){
		
		return relation;
	
	}

	public void addQuery(Query query) {
		
		if (queries == null){
			queries = new ArrayList<Query>();
		}
		
		queries.add(query);
		
	}
	
}